define(['app'], function(app)
{
    app.directive('hello',function(){
        return{
            restrict: 'E',
            template: '<span>么么哒~</span>',
            replace: true
        };
    });
    app.directive('exp', function() {
        return {
            restrict : 'EA',
            replace : true,
            transclude : true,
            scope : {
                title : '=expTitle'
            },
            template : '<div>'
                     + '<div class="title" ng-click="toggle()">{{title}}</div>'
                     + '<div class="body" ng-show="showMe" ng-transclude></div>'
                     + '</div>',
            link : function(scope, element, attrs) {
                scope.showMe = false;
                scope.toggle = function toggle() {
                    scope.showMe = !scope.showMe;
                }
            }
        }
    });

    var INTEGER_REGEXP = /^\-?\d*$/;
    app.directive('integer', function() {
      return {
        require: 'ngModel',  //请求另外的controller，传入当前directive的linking function中
        link: function(scope, elm, attrs, ctrl) {
          ctrl.$parsers.unshift(function(viewValue) {
            if (INTEGER_REGEXP.test(viewValue)) {
              ctrl.$setValidity('integer', true);
              return viewValue;
            } else {
              ctrl.$setValidity('integer', false);
              return undefined;
            }
          });
        }
      };
    });

    var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/;
    app.directive('smartFloat', function() {
      return {
        require: 'ngModel',
        link: function(scope, elm, attrs, ctrl) {
          ctrl.$parsers.unshift(function(viewValue) {
            if (FLOAT_REGEXP.test(viewValue)) {
              ctrl.$setValidity('float', true);
              return parseFloat(viewValue.replace(',', '.'));
            } else {
              ctrl.$setValidity('float', false);
              return undefined;
            }
          });
        }
      };
    });
});
